/*
 *  LanguagesComboBoxModel.java
 *
 *  Course Project for User Interface Programming I, 2011
 *  Group 19
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 */
package justdoit.view.model;

import java.util.Locale;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

/**
 * This class is a model that stores available languages.
 * 
 * @author Can
 */
public class LanguagesComboBoxModel
        extends AbstractListModel
        implements ComboBoxModel {

    private Locale[] languages;
    private Locale selectedLanguage;

    public LanguagesComboBoxModel(Locale[] langs) {
        this.languages = langs;
    }

    public Locale getSelectedLocale() {
        return selectedLanguage;
    }

    public void setSelectedItem(Object anItem) {
        for (Locale l : this.languages) {
            if (l.getDisplayName().equals(anItem)) {
                this.selectedLanguage = l;
                return;
            }
        }
        this.selectedLanguage = null;
    }

    public Object getSelectedItem() {
        if (this.selectedLanguage == null) {
            return null;
        }
        return this.selectedLanguage.getDisplayName();
    }

    public int getSize() {
        return languages.length;
    }

    public Object getElementAt(int index) {
        return this.languages[index].getDisplayName();
    }
}
